package com.specialized.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.specialized.auth.domain.CaptchaVos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.specialized.auth.form.LoginBody;
import com.specialized.auth.form.RegisterBody;
import com.specialized.auth.service.SysLoginService;
import com.specialized.common.core.domain.Result;
import com.specialized.common.core.utils.JwtUtils;
import com.specialized.common.core.utils.StringUtils;
import com.specialized.common.security.auth.AuthUtil;
import com.specialized.common.security.service.TokenService;
import com.specialized.common.security.utils.SecurityUtils;
import com.specialized.system.model.LoginUser;

/**
 * token 控制
 *
 * @author specialized
 */
@RestController
public class TokenController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private CaptchaService captchaService;

    @PostMapping({"/get"})
    public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) {
        assert request.getRemoteHost() != null;

        data.setBrowserInfo(getRemoteId(request));
        return this.captchaService.get(data);
    }
    @PostMapping({"/check"})
    public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) {
        data.setBrowserInfo(getRemoteId(request));
        return this.captchaService.check(data);
    }

    public ResponseModel verify(@RequestBody CaptchaVO data, HttpServletRequest request) {
        return this.captchaService.verification(data);
    }

    public static final String getRemoteId(HttpServletRequest request) {
        String xfwd = request.getHeader("X-Forwarded-For");
        String ip = getRemoteIpFromXfwd(xfwd);
        String ua = request.getHeader("user-agent");
        return com.anji.captcha.util.StringUtils.isNotBlank(ip) ? ip + ua : request.getRemoteAddr() + ua;
    }

    private static String getRemoteIpFromXfwd(String xfwd) {
        if (com.anji.captcha.util.StringUtils.isNotBlank(xfwd)) {
            String[] ipList = xfwd.split(",");
            return com.anji.captcha.util.StringUtils.trim(ipList[0]);
        } else {
            return null;
        }
    }


    @PostMapping("login")
    public Result<?> login(@RequestBody LoginBody form) {
        LoginUser userInfo=null;
        if(null==form.getPhonenumber()){
             userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        }
        // 用户登录
        else{
            userInfo=sysLoginService.loginCode(form.getPhonenumber(),form.getLogincode());
        }
        //手机号验证码登录

        // 获取登录token
        return Result.success(tokenService.createToken(userInfo));
    }

    @DeleteMapping("logout")
    public Result<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return Result.success();
    }

    @PostMapping("refresh")
    public Result<?> refresh(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return Result.success();
        }
        return Result.success();
    }

    @PostMapping("register")
    public Result<?> register(@RequestBody RegisterBody registerBody) {
        // 用户注册
        sysLoginService.register(registerBody);
        return Result.success();
    }

    @PostMapping("/getlogincode")
    public Result getlogincode(@RequestBody CaptchaVos captchaVo,HttpServletRequest request){
            return sysLoginService.getlogincode(captchaVo,request);
    }
}
